<?php 
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$setting=core_setting('zhuce');
if(!$setting['open']){
	exi('本站暂未开启注册！','error','core/home.applogin/');
}
if($_RQ['op']=='get'){
	if($_SESSION['os']!='windows'){
		exi('请在PC电脑端打开！','error');
	}
	if($_RQ['onesysname']){
		$_SESSION['user']['onesysname']=$_RQ['onesysname'];
	}
	$_SESSION['user']['onesysname']=$_SESSION['user']['onesysname']?:$_AR['system']['takeoverplugin'];
	$index_file=ROOT_PLUGIN.$_SESSION['user']['onesysname'].'/manage/actions/sys/register.php';
	if(is_file($index_file)){
		exi('','',$_SESSION['user']['onesysname'].'/sys.register/');
	}
	$page=core_setting('site');
	$plugin=pdo_get('core_plugins',array('name'=>$_SESSION['user']['onesysname']),array('id','title','description','image'));
	if(!$plugin['id']){
		unset($_SESSION['user']['onesysname']);
		unset($_SESSION['user']['isonesys']);
		exi('','',manage_url('core/user.login/',true));
	}
	$plugin['title']=str_replace('进云','系统',$plugin['title']);
	$plugin['description']=str_replace('进云','系统',$plugin['description']);
	$page['title']=$plugin['title']?:$page['title'];
	$page['logo']=$plugin['image']?:$page['logo'];
	$page['cname']=$plugin['description']?:$page['cname'];
	$agent = core_setting('agent');
	$return=array(
		'verify_code'=>$_AR['system']['verify_code'],
		'mobile_code'=>$_AR['system']['mobile_code'],
		'llogo'=>$page['llogo']?tomedia($page['llogo']):'../core/resource/images/ng-logo.png',
		'name'=>$page['name'],
		'ico'=>$operating['logo'],
		'item'=>array()
	);
	$operating=core_setting('operating');
	$return['operating']=$operating;
	$return['site']=$page;
	$return['open']=$agent['open'];
	$return['item']=array(
		'login_type'=>1,
	);
	set_html('header','');
	set_html('body','common/body-cms');
	set_html('title','登录');
	exi($return);
}
if($_RQ['op']=='register'){
	$post=get_postdata();
	if($_AR['system']['verify_code']==1 && !check_image_code($post['code'])) {
		exi('验证码错误！','error');
	}
	$member = array();
	if($post['login_type']==1){
		$member['name'] = trim($post['name']);
		if(!preg_match('/^[\x{4e00}-\x{9fa5}a-z\d_\.]{3,15}$/iu', $member['name'])){
			exi('必须输入用户名，格式为 3-15 位字符，可以包括汉字、字母（不区分大小写）、数字、下划线和句点。','error');
		}
		if(pdo_getcolumn('core_users',array('name' => $member['name']),'id')){
			exi('非常抱歉，此用户名已经被注册，你需要更换注册名称！','error');
		}
		if($post['password'] !=$post['password1']) {
			exi('两次密码输入不一致！','error');
		}
		$member['password'] = $post['password'];
		if(strlen($member['password']) < 8) {
			exi('必须输入密码，且密码长度不得低于8位。','error');
		}
		if($member['password']==$member['name']) {
			exi('输入的密码不能跟用户名相同','error');
		}
	}else{
		if(!$post['smscode'] || !check_sms_code($post['smscode'])) {
			exi('手机验证码错误！','error');
		}
		if(!$post['mobile']){
			exi('请先填写手机号！','error');
		}
		$check=pdo_getcolumn('core_users',array('mobile'=>$post['mobile']),'id');
		if($check){
			exi('该手机号码已注册，请更换！','error');
		}
		$member['mobile']=$post['mobile'];
		if(pdo_getcolumn('core_users',array('mobile' => $member['mobile']),'id')){
			exi('非常抱歉，此手机号已经被注册，你需要更换注册名称！','error');
		}
		$member['name']=$post['mobile'];
		if(pdo_getcolumn('core_users',array('name' => $member['name']),'id')){
			exi('非常抱歉，此用户名已经被注册，你需要更换注册名称！','error');
		}
	}
	$res=cfc('user')->create_user($member);
	if(is_error($res)){
		exi('注册失败！请稍后再试！','error');
	}
	if($post['incode']){
		$incode = $post['incode'];
		if($post['incode']){
			$uniacid = pdo_getcolumn('core_agent',array('incode'=>$incode),'uniacid');
		}
		if($uniacid){
			pdo_update('core_accounts',array('agent_id'=>$uniacid),array('uniacid'=>$res));
		}
	}
	$_SESSION['action']=array();
	exi('注册成功！请重新登陆！','','./index.php?p=core&action=home.applogin');
}
if($_RQ['op']=='send_code'){
	if(!$_AR['system']['mobile_code'] || !$_AR['system']['mobile_mb']){
		exi('系统暂不支持手机号码注册！','error');
	}
	
	$post=get_postdata();
	if($_AR['system']['verify_code']==1 && !check_image_code($post['code'])) {
		exi('图片验证码错误！','error');
	}
	$mobile=trim($post['mobile']);
	if(!$mobile){
		exi('手机号格式错误！','error');
	}
	$mobile="$mobile";
	if(strlen($mobile)!=11){
		exi('手机号不合法！','error');
	}
	$check=pdo_getcolumn('core_users',array('mobile'=>$mobile),'id');
	if($check){
		exi('该手机号用户已存在！请直接登陆或更换手机号！','error');
	}
	$_SESSION['smscode']=random(6,true);
	$res=cfc('jy_alisms')->send($mobile,array('code'=>$_SESSION['smscode']),$_AR['system']['mobile_mb']);
	if(is_error($res)){
		exi('短信发送失败！'.$res['message'],'error');
	}else{
		exi('success');
	}
}
